package com.godz.getter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.godz.getter.mapper.GoodsMapper;
import com.godz.getter.mapper.SellerMapper;
import com.godz.getter.pojo.Goods;
import com.godz.getter.pojo.Seller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class SellController {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    SellerMapper sellerMapper;

    String msgMone = "";

    @RequestMapping("/SellGoods")
    public String SellGoods(Model model) {
        model.addAttribute("disp", msgMone);
        msgMone = "";
        return "contact";
    }

    @PostMapping("/addGoods")
    public String Add(
            @RequestParam("goodsname") String goodsname,
            @RequestParam("name") String name,
            @RequestParam("stuId") String stuId,
            @RequestParam("class") String class1,
            @RequestParam("phone") String phone,
            @RequestParam("qq") String qq,
            @RequestParam("wechat") String wechat,
            @RequestParam("exampleRadios") Integer exampleRadios,
            @RequestPart("file1") MultipartFile file1,
            @RequestPart("file2") MultipartFile file2,
            @RequestPart("file3") MultipartFile file3,
            @RequestParam("address") String address,
            @RequestParam("message") String message,
            @RequestParam("goodsprice") Integer goodsprice
    ) {
        Goods goods = new Goods();//货
        Seller seller = new Seller();//卖家
        //部分数据给goods表
        goods.setGName(goodsname);
        goods.setGDetails(message);
        goods.setGStuId(stuId);
        goods.setPass(true);//显示主页
        goods.setGCategory(exampleRadios);
        goods.setGDate(new Date());
        goods.setGPrice(goodsprice);
        //部分数据给sellers表
        seller.setSelId(stuId);
        seller.setSelClass(class1);
        seller.setSelName(name);
        seller.setSelDormitory(address);
        seller.setSelQq(qq);
        seller.setSelWechat(wechat);
        seller.setSelPhone(phone);
        seller.setSelRec(1);
        //三个图片文件都不能为空
        if (!file1.isEmpty() && !file2.isEmpty() && !file3.isEmpty()) {
            //根据时间取名字
            SimpleDateFormat f = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
            String format = f.format(new Date());
            //保存文件到服务器
            String filename1 = format + file1.getOriginalFilename();
            String filename2 = format + file2.getOriginalFilename();
            String filename3 = format + file3.getOriginalFilename();
            String property = System.getProperty("user.dir");//得到当前路径
            try {
                file1.transferTo(new File(property + "\\src\\main\\resources\\static\\assets\\img\\portfolio\\" + filename1));
                file2.transferTo(new File(property + "\\src\\main\\resources\\static\\assets\\img\\portfolio\\" + filename2));
                file3.transferTo(new File(property + "\\src\\main\\resources\\static\\assets\\img\\portfolio\\" + filename3));
                //存到数据库中
                String lujing = "/assets/img/portfolio/";
                goods.setGImg1(lujing + filename1);
                goods.setGImg2(lujing + filename2);
                goods.setGImg3(lujing + filename3);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("三个图片有空");
        }

        goodsMapper.insert(goods);
        //根据SQL查询是否已经存在
        QueryWrapper<Seller> wrapper = new QueryWrapper<Seller>().eq("sel_id", stuId);
        Seller seller1 = sellerMapper.selectOne(wrapper);
        if (seller1 != null) {
            seller1.setSelRec(seller1.getSelRec() + 1);
            sellerMapper.update(seller1, wrapper);
        } else {
            System.out.println(seller);
            sellerMapper.insert(seller);
        }
        msgMone = "上传成功,需要等待后台审核,审核完成后即可显示";

        return "redirect:/SellGoods#success";
    }

}
